package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseContactsMapper;
import com.ly.heart.service.IKingBaseContactsService;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseContactsEntity;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 金蝶-基础-联系人Service业务层处理
 *
 * @author cxl
 * @date 2024-07-01
 */
@Service
public class KingBaseContactsServiceImpl extends ServiceImpl<KingBaseContactsMapper, KingBaseContactsEntity> implements IKingBaseContactsService {

    @Resource
    private KingBaseContactsMapper kingBaseContactsMapper;

    /**
     * 查询金蝶-基础-联系人
     *
     * @param id 金蝶-基础-联系人ID
     * @return 金蝶-基础-联系人
     */
    @Override
    public KingBaseContactsEntity selectKingBaseContactsEntityById ( String id ) {
        return kingBaseContactsMapper.selectKingBaseContactsEntityById ( id );
    }

    /**
     * 查询金蝶-基础-联系人列表
     *
     * @param kingBaseContactsEntity 金蝶-基础-联系人
     * @return 金蝶-基础-联系人
     */
    @Override
    public AjaxResult selectKingBaseContactsEntityList ( KingBaseContactsEntity kingBaseContactsEntity ) {
//        IPage iPage = this.page ( new Page<> ( kingBaseContactsEntity.getPage (), kingBaseContactsEntity.getLimit () ), new LambdaQueryWrapper<KingBaseContactsEntity> ().eq ( KingBaseContactsEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
//        return AjaxResult.success ( iPage );
        kingBaseContactsEntity.setIsUsed(ConstUtil.IS_USED_YES);
        List<KingBaseContactsEntity> list = kingBaseContactsMapper.selectKingBaseContactsEntityList(kingBaseContactsEntity);
        Page<KingBaseContactsEntity> page = new Page<>(kingBaseContactsEntity.getPage (), kingBaseContactsEntity.getLimit ());
        return AjaxResult.success(ReturnPage.getReturnPage(page,list));
    }

    /**
     * 新增金蝶-基础-联系人
     *
     * @param kingBaseContactsEntity 金蝶-基础-联系人
     * @return 结果
     */
    @Override
    public int insertKingBaseContactsEntity ( KingBaseContactsEntity kingBaseContactsEntity ) {
        kingBaseContactsEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBaseContactsEntity.setCreateTime ( DateUtils.getNowDate () );
        kingBaseContactsEntity.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        return kingBaseContactsMapper.insertKingBaseContactsEntity ( kingBaseContactsEntity );
    }

    /**
     * 修改金蝶-基础-联系人
     *
     * @param kingBaseContactsEntity 金蝶-基础-联系人
     * @return 结果
     */
    @Override
    public int updateKingBaseContactsEntity ( KingBaseContactsEntity kingBaseContactsEntity ) {
        return kingBaseContactsMapper.updateKingBaseContactsEntity ( kingBaseContactsEntity );
    }

    /**
     * 批量删除金蝶-基础-联系人
     *
     * @param ids 需要删除的金蝶-基础-联系人ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseContactsEntityByIds ( String[] ids ) {
        List<KingBaseContactsEntity> list = this.list ( new LambdaQueryWrapper<KingBaseContactsEntity> ().in ( KingBaseContactsEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( contact -> contact.getIsDel () == 1 ).map ( KingBaseContactsEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( contact -> {
            if ( contact.getIsDel () == 2 ) {
                stringBuilder.append ( contact.getName () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseContactsEntity> ().set ( KingBaseContactsEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseContactsEntity :: getChangeTime, DateUtils.getDate () ).
                in ( KingBaseContactsEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除联系人失败！" );
        }
        String str = "删除联系人成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-基础-联系人信息
     *
     * @param id 金蝶-基础-联系人ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseContactsEntity baseContactsEntity = this.getById ( id );
        if ( baseContactsEntity == null ) {
            throw new CustomException ( "无此联系人！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseContactsEntity> ().
                set ( KingBaseContactsEntity :: getIsDisabled, baseContactsEntity.getIsDisabled () == 1 ? 2 : 2 ).
                set ( KingBaseContactsEntity :: getChangeTime, DateUtils.getDate () ).
                eq ( KingBaseContactsEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( baseContactsEntity.getIsDisabled () == 1 ? "禁用联系人失败！" : "解禁联系人失败！" );
        }
        return AjaxResult.success ();
    }

}
